/**
 * @Author: Zerone
 *
 * So tired. I spent enormous time understanding and implementing the KNN algorithm.
 * However, I have learned a lot simultaneously.
 */

import database.DataBase;
import mail.Email;

import javax.mail.Session;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void cls() {
        for (int i = 0; i < 10; ++i) {
            System.out.println();
        }
    }

    public static void showMenu() {
        System.out.println("|----------------------------------------------------|");
        System.out.println("|             Email Assistant is running...          |");
        System.out.println("|                                                    |");
        System.out.println("|              1.Send mails automatically.           |");
        System.out.println("|              2.Show all emails.                    |");
        System.out.println("|              3.Semantic Analysis.                  |");
        System.out.println("|              0.Exit.                               |");
        System.out.println("|----------------------------------------------------|");
        System.out.print("\nInput your choice: ");
    }

    /**
     * Read addresses from the database and send to them.
     */
    public static void sender() throws Exception {
        ArrayList<String> addresses = DataBase.getAddresses();
        Session session = Email.getSendSession();

        for (int i = 0; i < addresses.size(); ++i) {
            Email.sendMail(addresses.get(i), session);
            Thread.sleep(2000);
        }
    }

    private static void semanticAnalysis() throws Exception {
        int count = Email.getMailsCount();
        if (count != 0) {
            for (int i = 0; i < count; ++i) {
                Email.showMail(i, false);
            }
        } else {
            System.out.println("No emails.");
        }
    }

    private static void showAllMails() throws Exception {
        int count = Email.getMailsCount();
        System.out.println("Inbox: " + count);
        if (count != 0) {
            for (int i = 0; i < count; ++i) {
                Email.showMail(i,true);
            }
        } else {
            System.out.println("No emails.");
        }
    }

    public static void getChoice() throws Exception {
        Scanner in = new Scanner(System.in);
        String choice = in.next();
        System.out.println("\n----------------------------------------------------\n");
        switch (choice) {
            case "1":
                sender();
                break;
            case "2":
                showAllMails();
                break;
            case "3":
                semanticAnalysis();
                break;
            case "0":
                System.exit(0);
                break;
        }
        System.out.println("\n----------------------------------------------------\n");
    }

    public static void main(String[] args) {
        /**
         * Create a thread to listen to the inbox.
         * Inform the user at once when a new mail arrives.
         */
        Thread receiver = new Thread(new Receiver(10000));
        receiver.start();

        while (true) {
            showMenu();
            try {
                getChoice();
            } catch (Exception e) {
                e.printStackTrace();
            }
            cls();
        }
    }
}

class Receiver implements Runnable {
    // The interval of checking the inbox.
    private int interval;

    public Receiver(int interval) {
        this.interval = interval;
    }

    /**
     * By checking whether the number of emails changed
     * to determine whether to receive new mail.
     */
    @Override
    public void run() {
        try {
            while (true) {
                if (Email.receiveMail()) {
                    JOptionPane.showMessageDialog(null,
                            "You have a new email !", "Prompt",
                            JOptionPane.CLOSED_OPTION);
                }

                // Check the mailbox once every ten seconds
                Thread.sleep(interval);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}